import { convertCamcelToPascal } from '@/utilities/common';

// 针对 ElementPlus 的表格事件通用处理器
// param 表格的查询参数
// func 查询表格的函数
export function CommonElePlusTableEventHandler(param, func) {
  this.param = param;
  this.func = func;

  this.onSortChange = (sortWay) => {
    if (!sortWay.order) return;

    this.param.value.sortField = convertCamcelToPascal(sortWay.prop);
    this.param.value.isAsc = sortWay.order === 'ascending';
    this.func();
  };

  this.onSizeChange = (size) => {
    // 如果每页条数变了，则需要从第1页开始（因为总页数可能已经变化了）
    this.param.value.pageIndex = 1;
    this.param.value.pageSize = size;
    this.func();
  };

  this.onCurrentChange = (page) => {
    // 页码变化了，需要根据最新的当前页渲染数据
    this.param.value.pageIndex = page;
    this.func();
  };

  this.onSearch = () => {
    this.param.value.pagenum = 1; // 搜索时需要重置页码
    this.func();
  };

  this.onResetFunc = (resetFunc) => {
    return () => {
      this.param.value.pagenum = 1; // 重置时也需要重置页码
      resetFunc();
      this.func();
    };
  };
}
